package cn.myschool.java79.util;

import java.sql.*;
import java.util.Properties;

/**
 * @author oldliu
 * @since 1.0
 * 链接的工具类
 */
public class DBUtil {
//    public static String  url = "jdbc:mysql://localhost:3306/java79school?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
//
//    public static String driver="com.mysql.jdbc.Driver";
//    public static String user="root";
//    public static String pass="root1234";
public static String  url ;
    public static String driver;
    public static String user;
    public static String pass;
    static {
        Properties p=new Properties();

        try {
            p.load(DBUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
            driver=p.getProperty("driver");
            url=p.getProperty("url");
            user=p.getProperty("user");
            pass=p.getProperty("pass");

            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private DBUtil(){}
    public static Connection getConnection()throws SQLException {
        return DriverManager.getConnection(url,user,pass);
    }
    public static void close(Connection con){
        if(con!=null) {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(ResultSet con){
        if(con!=null) {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement con){
        if(con!=null) {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(PreparedStatement con){
        if(con!=null) {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet rs,Statement st,Connection con){
        close(rs);
        close(st);
        close(con);
    }
    public static void close(ResultSet rs,PreparedStatement st,Connection con){
        close(rs);
        close(st);
        close(con);
    }
    public static void close(PreparedStatement st,Connection con){
        close(st);
        close(con);
    }
}
